package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"strconv"
	"time"
	"tjins-foodie/cache"
	"tjins-foodie/models"
)

func CarAdd(c *gin.Context) {
	car_no := c.PostForm("car_no")
	car_status := c.PostForm("car_status")
	car_num := c.PostForm("car_num")
	data := models.Car{
		CarNo:     car_no,
		CarStatus: car_status,
		CarNum:    car_num,
	}
	models.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func CarOrderAdd(c *gin.Context) {
	cid, _ := strconv.Atoi(c.PostForm("cid"))
	car_order_no := c.PostForm("car_order_no")
	car_order_name := c.PostForm("car_order_name")
	car_order_price, _ := strconv.Atoi(c.PostForm("car_order_price"))
	data := models.CarOrder{
		Cid:           cid,
		CarOrderNo:    car_order_no,
		CarOrderName:  car_order_name,
		CarOrderPrice: car_order_price,
	}
	models.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}
func CarOrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "5"))
	offset := (page - 1) * size
	var list []models.CarOrderList
	redis_key := "car_list_" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		/*
			SELECT
			car_orders.id as OrderId,
			cars.car_no as  CarNo,
			cars.car_status as CarStatus,
			cars.car_num as CarNum,
			car_orders.car_order_no as CarOrderNo,
			car_orders.car_order_name as CarOrderName,
			car_orders.car_order_price as CarOrderPrice
			FROM
			car_orders
			LEFT JOIN cars on cars.id=car_orders.cid
		*/
		models.DB.Table("car_orders").
			Select("car_orders.id as OrderId",
				"cars.car_no as  CarNo",
				"cars.car_status as CarStatus",
				"cars.car_num as CarNum",
				"car_orders.car_order_no as CarOrderNo",
				"car_orders.car_order_name as CarOrderName",
				"car_orders.car_order_price as CarOrderPrice").Joins("LEFT JOIN cars on cars.id=car_orders.cid").Offset(offset).Limit(size).Debug().Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	var total int64
	models.DB.Model(&models.CarOrder{}).Count(&total)
	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "查询成功",
		"data":  list,
		"total": total,
	})
	return

}
func CarOrderDetail(c *gin.Context) {
	id := c.Query("id")
	var list models.CarOrderList
	redis_key := "car_detail_" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		/*
			SELECT
			car_orders.id as OrderId,
			cars.car_no as  CarNo,
			cars.car_status as CarStatus,
			cars.car_num as CarNum,
			car_orders.car_order_no as CarOrderNo,
			car_orders.car_order_name as CarOrderName,
			car_orders.car_order_price as CarOrderPrice
			FROM
			car_orders
			LEFT JOIN cars on cars.id=car_orders.cid
		*/
		models.DB.Table("car_orders").
			Select("car_orders.id as OrderId",
				"cars.car_no as  CarNo",
				"cars.car_status as CarStatus",
				"cars.car_num as CarNum",
				"car_orders.car_order_no as CarOrderNo",
				"car_orders.car_order_name as CarOrderName",
				"car_orders.car_order_price as CarOrderPrice").Joins("LEFT JOIN cars on cars.id=car_orders.cid").Where("car_orders.id=?", id).
			Debug().Find(&list)
		jsonData, _ := json.Marshal(list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "查询成功",
		"data": list,
	})
	return

}
